package com.kfm.pm.exec;

import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.TreeSet;

public class Exec03 {

    public static void main(String[] args) {

        // 创建一个HashSet，包含一些单词。编写代码，找到集合中最长的单词。
        HashSet<String> stringHashSet = new HashSet<>();
        stringHashSet.add("hello");
        stringHashSet.add("jav");
        stringHashSet.add("kfm");
        stringHashSet.add("kaifamiao");
        stringHashSet.add("genshin");
        stringHashSet.add("pomelo");
//        int length = 0;
        String s = "";
        Iterator<String> iterator = stringHashSet.iterator();
        while (iterator.hasNext()) {
            String str = iterator.next();
            if (str.length() > s.length()) {
                s = str;
            }
        }
        System.out.println(s);
        TreeSet<String> treeSet = new TreeSet<>(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                if (o1.equals(o2)) {
                    return 0;
                }
                return o1.length() - o2.length() > 0 ? 1 : -1;
            }
        });
        treeSet.addAll(stringHashSet);
//        System.out.println(treeSet.last());
//        System.out.println(treeSet);
    }
}
